package com.chenziruo.securitydemo.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenziruo.securitydemo.entity.User;
import com.chenziruo.securitydemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: DBUserDetailsManager
 * Description:
 * 模仿InMemoryUserDetailsManager创建一个基于内存的
 *
 * @author chenziruo
 * @since 2024-06-14 10:57:31
 */
@Component
public class DBUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }

    @Override
    public void createUser(UserDetails userDetails) {
        User user = new User();
        user.setUsername(userDetails.getUsername());
        user.setPassword(userDetails.getPassword());
        user.setEnabled(true);
        userMapper.insert(user);
    }

    @Override
    public void updateUser(UserDetails user) {

    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }

    /**
     * 从数据库中获取用户信息
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        } else {
            List<GrantedAuthority> authorities = new ArrayList<>();
//            authorities.add(()->{
//               return  "USER_LIST";
//            });
            authorities.add(() -> {
                return "USER_ADD";
            });
//            return new org.springframework.security.core.userdetails.User(
//                    user.getUsername(),
//                    user.getPassword(),
//                    user.getEnabled(),
//                    true,//用户账号是否过期
//                    true,//用户凭证是否过期
//                    true,//用户是否被锁定
//                    authorities
//            );//权限列表
            return org.springframework.security.core.userdetails.User
                    .withUsername(user.getUsername())
                    //使用默认的密码加密方案
                    .password(user.getPassword())
                    .disabled(!user.getEnabled())
                    .roles("ADMINISTRATOR")
                    .build();
        }
    }
}